package com.gome.oa.dialog;

import java.awt.BorderLayout;
import java.util.Calendar;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import com.gome.oa.HolidayManagement;
import com.gome.oa.custom.CustomCheckbox;
import com.gome.oa.custom.FormTableModel;
import com.gome.oa.custom.GMBrowser;
import com.gome.oa.custom.JPanelWithHeight;
import com.gome.oa.custom.editor.CheckboxTableCellEditor;
import com.gome.oa.custom.editor.ComBoxTableCellEditor;
import com.gome.oa.custom.editor.DatetimeTableCellEditor;
import com.gome.oa.custom.render.CheckboxTableCellRenderer;
import com.gome.oa.custom.render.ComBoxCellRenderer;
import com.gome.oa.custom.render.DatetimeCellRenderer;
import com.gome.oa.listener.CheckboxListener;
import com.gome.oa.listener.ConfirmActionListener;
import com.gome.oa.listener.DisplayTypeActionListener;
import com.gome.oa.listener.FormModelListener;
import com.gome.oa.model.Day;
import com.gome.oa.model.SelectType;
import com.gome.oa.util.DateUtil;
import com.teamdev.jexplorer.Browser;

/**
 *
 * @author: tang li
 * @date: 2016年8月20日
 * @desc: 
 *
 */
public class FillFormDialog extends JDialog {

	private static final long serialVersionUID = -4426532326713374956L;
	
	private List<Day> days;
	
	private JTable tblForm = new JTable();
	
	private JButton btnConfirm = new JButton("确认");
	
	private SelectType[] selectTypes = {new SelectType("1", "申请普通加班费"),
			new SelectType("2", "申请节假加班费"),
			new SelectType("3", "申请存休")};
	
	private DefaultTableModel dtm = new FormTableModel();
	
	public FillFormDialog(Browser browser, PopupBrowserDialog popupDialog, List<Day> days) {
		super(popupDialog, true);
		this.days = days;
		this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
		this.setSize(800, 600);
		this.setLocationRelativeTo(null);
		
		JScrollPane scrollPane = new JScrollPane(tblForm);
		JPanel row1 = new JPanelWithHeight(400, new BorderLayout());
		row1.add(scrollPane, BorderLayout.CENTER);
		this.getContentPane().add(row1);
		this.getContentPane().add(btnConfirm);
		
		tblForm.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
		tblForm.getTableHeader().setReorderingAllowed(false);
		
		Object[] headers = {"", "加班类型", "开始日期", "(加班)开始时间", "(加班)结束时间", "时间总计", "加班事由"};
		dtm.setColumnIdentifiers(headers);
		tblForm.setModel(dtm);
		tblForm.setPreferredScrollableViewportSize(tblForm.getPreferredSize());
		
		DefaultCellEditor singleclick = new DefaultCellEditor(new JTextField());
	    singleclick.setClickCountToStart(1);
	    //set the editor as default on every column
	    tblForm.setDefaultEditor(tblForm.getColumnClass(6), singleclick);
	    
	    tblForm.getColumnModel().getColumn(0).setCellRenderer(new CheckboxTableCellRenderer());
	    tblForm.getColumnModel().getColumn(0).setCellEditor(new CheckboxTableCellEditor());
	    tblForm.getColumnModel().getColumn(1).setCellRenderer(new ComBoxCellRenderer());
	    tblForm.getColumnModel().getColumn(1).setCellEditor(new ComBoxTableCellEditor());
	    tblForm.getColumnModel().getColumn(3).setCellRenderer(new DatetimeCellRenderer());
	    tblForm.getColumnModel().getColumn(3).setCellEditor(new DatetimeTableCellEditor());
	    tblForm.getColumnModel().getColumn(3).setPreferredWidth(100);
	    
	    tblForm.getModel().addTableModelListener(new FormModelListener(this));
	    tblForm.setAutoCreateRowSorter(true);
	    for (int column = 0 ; column < dtm.getColumnCount() ; column++) {
	    	((TableRowSorter<?>) tblForm.getRowSorter()).setSortable(column, false);
	    }
	    
		btnConfirm.addActionListener(new ConfirmActionListener(this, tblForm, (GMBrowser)browser));
		
		initTableData();
	}

	private void initTableData() {
		
		HolidayManagement holidayManagement = new HolidayManagement();
		CheckboxListener checkBoxListener = new CheckboxListener(this, dtm);
		
		for(Day day : days) {
			if(holidayManagement.isHoliday(day.getMinDate())) {
				day.setOffDate(day.getMinDate());
			}
			else {
				Calendar offworkTime = Calendar.getInstance();
				offworkTime.setTime(day.getMinDate());
				offworkTime.add(Calendar.HOUR_OF_DAY, 9);
				day.setOffDate(offworkTime.getTime());
			}
			calculateHour(day);
			
			String startDate = DateUtil.format1(day.getMinDate()) + "(" + DateUtil.format3(day.getMinDate()) + ")";
			//String startTime = DateUtil.format3(day.getOffDate());
			String endTime = DateUtil.format3(day.getMaxDate());
			CustomCheckbox checkbox = new CustomCheckbox("", true, checkBoxListener, day, true);
			JComboBox<SelectType> comboBox = new JComboBox<SelectType>(selectTypes);
			comboBox.setSelectedIndex(2);
			comboBox.setBorder(null);
			dtm.addRow(new Object[]{checkbox, comboBox, startDate, day.getOffDate(), endTime, day.getHour(), ""});
		}
		
		//合计
		final JComboBox<SelectType> comboBoxSummary = new JComboBox<SelectType>(new SelectType[]{new SelectType("1", "只显示加班"), new SelectType("2", "显示全部")});
		comboBoxSummary.setBorder(null);
		comboBoxSummary.addActionListener(new DisplayTypeActionListener(comboBoxSummary, tblForm, this));
		dtm.addRow(new Object[]{new CustomCheckbox("全选", true, checkBoxListener, null, true), comboBoxSummary, "", null, "", "", ""});
		comboBoxSummary.setSelectedIndex(0);
		
		updateHours();
	}
	
	public void calculateHour(Day day) {
		if(day.getMaxDate() != null) {
			long diff = (day.getMaxDate().getTime() - day.getOffDate().getTime())/1000;
			int hour = (int)diff/3600;
			day.setHour(hour);
		}
	}
	
	public void updateHours() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				int totalHour = calculateSelectedHour();
				dtm.setValueAt("<html><span style='color:blue;'><b>" + totalHour + "</b></span></html>", dtm.getRowCount() - 1, 5);
			}
		});
	}
	
	private int calculateSelectedHour() {
		int totalHour = 0;
		for(int i = 0; i < dtm.getRowCount() - 1; i++) {
			CustomCheckbox checkbox = (CustomCheckbox)dtm.getValueAt(i, 0);
			if(!checkbox.isSelected()) {
				continue;
			}
			if(!checkbox.isVisableInRow()) {
				continue;
			}
			totalHour += checkbox.getDay().getHour();
		}
		return totalHour;
	}

}
